输入char str[20]; 怎样才会不溢位??(c++语言)

来源:百度知道 编辑:UC知道 时间:2024/05/16 07:18:19
#include <iostream>
using namespace std;
int main(){
char str[20];
cin>>str;
}

如何限制只输入20个字符就停止输入 或者不会溢位???
如何限制输入数字在1至50的范围内???

char str[20];
cin.getline(str, 20);

#include <iostream>
using namespace std;
int main()
{
char str[20];
cin.getline(str, 20);
for(int i=0;i<20;i++)
{
cout<<str[i]<<" ";
}
}
/*
测试结果:
asdfljfjsldk2923742kdshjsdklfsdfasdfjjglsdld
a s d f l j f j s l d k 2 9 2 3 7 4 2
*/